﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.SqlClient;

public partial class home_GetWork : System.Web.UI.Page
{
    public int userId = 0;
    int managerId = 0;
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Session["id_user"] == null || Session["permission"] == null || ((int)Session["permission"]) <= 1
            || ((int)Session["permission"]) >= 4)
            Response.Redirect("../login.aspx");

        try
        {
            userId = Int32.Parse(Request.QueryString["user"]);
            if (userId <= 0)
                throw new Exception();
        }
        catch
        {
            userId = 0;
        }
        if (userId == 0)
            return;
        //get user name
        string sql = "SELECT username FROM [user] WHERE id=" + comm.to_sql(userId.ToString(), "number");
        DataTable dtTable = dal.SelectTable(sql);
        if (dtTable.Rows.Count != 1)
            return;
        username.InnerHtml = dtTable.Rows[0]["username"].ToString();

        #region check current user and user with userId have both register
        // get parentId of userId
        int parentId = -1;
        sql = "SELECT parentId FROM [user] WHERE id=" + comm.to_sql(userId.ToString(), "number");
        dtTable = dal.SelectTable(sql);
        if (dtTable.Rows.Count != 1)
            return;        
        parentId = Int32.Parse(dtTable.Rows[0]["parentId"].ToString());
        if (parentId == -1)
            return;
        // check parentId of userId and current user
        if (parentId != 0 && ((int)Session["permission"]) == 2) // userId is not userId of register and current user is register
        {
            if (parentId != Int32.Parse(Session["id_user"].ToString()))
                return;
        }
        if (parentId != 0 && ((int)Session["permission"]) == 3) //  userId is not userId of register and current user is manager
        {
            managerId = Int32.Parse(Session["id_user"].ToString());
            sql = "SELECT parentId FROM [user] WHERE id=" + comm.to_sql(managerId.ToString(), "number");
            dtTable = dal.SelectTable(sql);
            if (dtTable.Rows.Count != 1)
                return;
            if (parentId != Int32.Parse(dtTable.Rows[0]["parentId"].ToString()))
                return;
        }

        if (parentId == 0 && ((int)Session["permission"]) == 2) // userId is userId of register and current user is register
        {
            if (userId != Int32.Parse(Session["id_user"].ToString()))
                return;
        }

        if (parentId == 0 && ((int)Session["permission"]) == 3) //  userId is userId of register and current user is manager
        {
            managerId = Int32.Parse(Session["id_user"].ToString());
            sql = "SELECT parentId FROM [user] WHERE id=" + comm.to_sql(managerId.ToString(), "number");
            dtTable = dal.SelectTable(sql);
            if (dtTable.Rows.Count != 1)
                return;
            if (userId != Int32.Parse(dtTable.Rows[0]["parentId"].ToString()))
                return;
        }
        #endregion
        
        if (!IsPostBack)
        {
            BindData();
        }
    }

    private void BindData()
    {
        try
        {
            userId = Int32.Parse(Request.QueryString["user"]);
            if (userId <= 0)
                throw new Exception();
        }
        catch
        {
            userId = 0;
        }
        if (userId == 0)
            return;
        managerId = Int32.Parse(Session["id_user"].ToString());

        #region Load list project the user doesn't join
        SqlParameter[] listParams = new SqlParameter[2];
        // -- param 1: @userId
        SqlParameter param = new SqlParameter("@userId", userId);
        param.SqlDbType = SqlDbType.Int;
        param.Direction = ParameterDirection.Input;
        listParams[0] = param;

        // -- param 2: @managerId
        param = new SqlParameter("@managerId", managerId);
        param.SqlDbType = SqlDbType.Int;
        param.Direction = ParameterDirection.Input;
        listParams[1] = param;

        DataTable dtTable = dal.SelectTableSP("proProjectDontJoint_SEL", listParams);

        listprojects.DataSource = dtTable;
        listprojects.DataValueField = "id";
        listprojects.DataTextField = "name";
        listprojects.DataBind();

        #endregion

        #region Load list project the user joined
        // -- param 1: @userId
        SqlParameter[] someParams = new SqlParameter[2];
        param = new SqlParameter("@userId", userId);
        param.SqlDbType = SqlDbType.Int;
        param.Direction = ParameterDirection.Input;
        someParams[0] = param;

        // -- param 2: @managerId
        param = new SqlParameter("@managerId", managerId);
        param.SqlDbType = SqlDbType.Int;
        param.Direction = ParameterDirection.Input;
        someParams[1] = param;

        dtTable = dal.SelectTableSP("proProjectJoin_SEL", someParams);
        listjoinedproject.DataSource = dtTable;
        listjoinedproject.DataValueField = "id";
        listjoinedproject.DataTextField = "name";
        listjoinedproject.DataBind();
        #endregion
    }
    
    protected void submit_Click(object sender, EventArgs e)
    {
        string[] projects = comm.RemoveSpace(selectedprojects.Value).Split(new char[] { ',' },StringSplitOptions.RemoveEmptyEntries);

        #region add a user to projects
        for (int i = 0; i < projects.Length; i++)
        {
            try
            {
                SqlParameter[] listParams = new SqlParameter[3];
                // -- param 1: @userId
                SqlParameter param = new SqlParameter("@userId", userId);
                param.SqlDbType = SqlDbType.Int;
                param.Direction = ParameterDirection.Input;
                listParams[0] = param;

                // -- param 2: @managerId
                param = new SqlParameter("@managerId", managerId);
                param.SqlDbType = SqlDbType.Int;
                param.Direction = ParameterDirection.Input;
                listParams[1] = param;

                int projectId = Int32.Parse(projects[i]);
                // -- param 2: @projectId
                param = new SqlParameter("@projectId", projectId);
                param.SqlDbType = SqlDbType.Int;
                param.Direction = ParameterDirection.Input;
                listParams[2] = param;

                // add a user to a project
                dal.ExecuteSP("proAddUserToProject_GET", listParams);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        #endregion


        #region delete a user from projects
        projects = comm.RemoveSpace(unselectedprojects.Value).Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
        for (int i = 0; i < projects.Length; i++)
        {
            try
            {
                SqlParameter[] listParams = new SqlParameter[3];
                // -- param 1: @userId
                SqlParameter param = new SqlParameter("@userId", userId);
                param.SqlDbType = SqlDbType.Int;
                param.Direction = ParameterDirection.Input;
                listParams[0] = param;

                // -- param 2: @managerId
                param = new SqlParameter("@managerId", managerId);
                param.SqlDbType = SqlDbType.Int;
                param.Direction = ParameterDirection.Input;
                listParams[1] = param;

                int projectId = Int32.Parse(projects[i]);
                // -- param 2: @projectId
                param = new SqlParameter("@projectId", projectId);
                param.SqlDbType = SqlDbType.Int;
                param.Direction = ParameterDirection.Input;
                listParams[2] = param;

                // add a user to a project
                dal.ExecuteSP("proDeleteUserFromProject_GET", listParams);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        #endregion
        
        BindData();
    }
}
